package com.ucampus.controller;

import com.ucampus.dao.ApiResponse;
import com.ucampus.service.VerificationCodeService;
import com.ucampus.service.userRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能：
 * 作者： 启明星_Polaris
 * 日期：2025/1/7 下午3:13
 **/

@RestController
@RequestMapping("/register")
public class userRegisterController {
    @Autowired
    private userRegisterService userRegisterService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    // 接收验证码接口
    @PostMapping("/storeCode")
    public ResponseEntity<?> storeCode(@RequestParam String userPhone, @RequestParam String code) {
        try {
            verificationCodeService.storeCode(userPhone, code);
            return ResponseEntity.status(HttpStatus.OK)
                    .body(new ApiResponse(200, "验证码已存储", null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(500, "存储验证码失败，请稍后再试", null));
        }
    }

    // 用户注册接口
    @PostMapping("/user")
    public ResponseEntity<?> register(@RequestParam String userPhone, @RequestParam String userpassword, @RequestParam String code) {
        boolean isRegistered = userRegisterService.registerUser(userPhone, userpassword, code);
        if (isRegistered) {
            return ResponseEntity.status(HttpStatus.OK)
                    .body(new ApiResponse(200, "注册成功", null));
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(new ApiResponse(400, "注册失败，请重试！", null));
        }
    }
}
